﻿using Knight.Web.Infrastructure;
using Knight.Web.Infrastructure.Menus;
using Knight.Web.Infrastructure.Mvc;

using Knight.Models.Entity;
using Knight.Models.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApp.Areas.Manage.Controllers.Filters;
using Knlght.Domain;

namespace WebApp.Areas.Manage.Controllers
{
    /// <summary>
    /// 友情链接
    /// </summary>
    public class LinkController : BaseAdminController
    {
        [MallAdminMenu("Link_Info")]
        [Permission("Link_Info")]
        public ActionResult Index()
        {
            return View();
        }

        #region AngularJs      
        [HttpPost]
        public JsonResult Paging(PagingParameter parameter)
        {
            var query = new QueryRequest<LinkInfo>();
            if (parameter.Query.CreateCriterionByString(nameof(LinkInfo.Name)) != null)
            {
                var username = parameter.Query.CreateCriterionByString(nameof(LinkInfo.Name)).Value.ToString();
                query.Add(o => o.Name.Contains(username));
            }
            if (parameter.Query.CreateCriterionByBoolean(nameof(LinkInfo.IsEnabled)) != null)
            {
                var isEnabled = Convert.ToBoolean(parameter.Query.CreateCriterionByBoolean(nameof(LinkInfo.IsEnabled)).Value);
                query.Add(o => o.IsEnabled == isEnabled);
            }
            if (parameter.Query.CreateCriterionByBoolean(nameof(LinkInfo.IsBlank)) != null)
            {
                var isBlank = Convert.ToBoolean(parameter.Query.CreateCriterionByBoolean(nameof(LinkInfo.IsBlank)).Value);
                query.Add(o => o.IsBlank == isBlank);
            }          
            query.AddOrder(nameof(LinkInfo.Name), true);
            query.AddOrder(nameof(LinkInfo.AddTime), false);

            var pagingRequest = new PagingRequest<LinkInfo>
            {
                PageIndex = parameter.PageIndex,
                PageSize = parameter.PageSize,
                Query = query
            };
            var service = getDefaultService();
            var rsp = service.Paging(pagingRequest);
            return EJPagingJson(rsp);
        }
        [HttpPost]
        public JsonResult Add(LinkInfo model, string tempImageUrl)
        {
            model.ImageUrl = UploadHelper.MoveFile(tempImageUrl);
            var service = getDefaultService();
            var rsp = service.Add(model);
            return rsp.IsSuccess ? EJActionJson(true, model.Id.ToString()) : EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult Update(LinkInfo model, string tempImageUrl)
        {
            model.ImageUrl = UploadHelper.MoveFile(tempImageUrl);
            var service = getDefaultService();
            var rsp = service.Update(model);
            return EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult Updates(int[] ids, string property, object value)
        {
            var service = getDefaultService();
            if (property.ToLower() == nameof(LinkInfo.IsEnabled).ToLower())
            {               
                var rsp = service.UpdateToList<LinkInfo>(o => ids.Contains(o.Id), i => new LinkInfo { IsEnabled = (bool)value });
                return EJActionJson(rsp);
            }
            else if (property.ToLower() == nameof(LinkInfo.IsBlank).ToLower())
            {             
                var rsp = service.UpdateToList<LinkInfo>(o => ids.Contains(o.Id), i => new LinkInfo { IsBlank = (bool)value });
                return EJActionJson(rsp);
            }
            else
            {
                return EJActionJson(false, "该属性修改方法没有实现！");
            }
        }
        [HttpPost]
        public JsonResult Remove(int id)
        {
            var service = getDefaultService();
            var rsp = service.Remove<LinkInfo>(o => o.Id == id);
            return EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult Removes(int[] ids)
        {
            var service = getDefaultService();
            var rsp = service.Remove<LinkInfo>(o => ids.Contains(o.Id));
            return EJActionJson(rsp);
        }
        const string AdUploadDirectory = "link";
        /// <summary>
        /// 上传文件
        /// </summary>
        [HttpPost]
        public JsonResult Upload()
        {
            var rsp = this.UploadPics(AdUploadDirectory);
            return EJItemJson(rsp);
        }
        #endregion
    }
}